package beautiful.butterfly.drds.schedule;

import beautiful.butterfly.drds.schedule.strategy.factory.ManageFactory;
import beautiful.butterfly.drds.schedule.task.IDataManager;
import beautiful.butterfly.drds.schedule.zoo_keeper.StrategyDataManager;
import beautiful.butterfly.drds.schedule.zoo_keeper.ZooKeeperManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Manager {
    protected static transient Logger logger = LoggerFactory.getLogger(Manager.class);

    public final static String configFile = System.getProperty("user.dir") + File.separator
            + "pamirsScheduleConfig.properties";

    private static ManageFactory manageFactory;

    public static boolean isInitial() throws Exception {
        return manageFactory != null;
    }

    public static boolean initial() throws Exception {
        if (manageFactory != null) {
            return true;
        }
        File file = new File(configFile);
        manageFactory = new ManageFactory();
        manageFactory.start = false;

        if (file.exists() == true) {
            //Console��������������
            Properties properties = new Properties();
            FileReader fileReader = new FileReader(file);
            properties.load(fileReader);
            fileReader.close();
            manageFactory.init(properties);
            logger.info("����Schedule�����ļ���" + configFile);
            return true;
        } else {
            return false;
        }
    }

    public static ManageFactory getManageFactory() throws Exception {
        if (isInitial() == false) {
            initial();
        }
        return manageFactory;
    }

    public static IDataManager getScheduleDataManager() throws Exception {
        if (isInitial() == false) {
            initial();
        }
        return manageFactory.getScheduleDataManager();
    }

    public static StrategyDataManager getScheduleStrategyManager() throws Exception {
        if (isInitial() == false) {
            initial();
        }
        return manageFactory.getScheduleStrategyManager();
    }

    public static Properties loadConfig() throws IOException {
        File file = new File(configFile);
        Properties properties;
        if (file.exists() == false) {
            properties = ZooKeeperManager.createProperties();
        } else {
            properties = new Properties();
            FileReader fileReader = new FileReader(file);
            properties.load(fileReader);
            fileReader.close();
        }
        return properties;
    }

    public static void saveConfigInfo(Properties properties) throws Exception {
        try {
            FileWriter fileWriter = new FileWriter(configFile);
            properties.store(fileWriter, "");
            fileWriter.close();
        } catch (Exception ex) {
            throw new Exception("����д��������Ϣ���ļ���" + configFile, ex);
        }
        if (manageFactory == null) {
            initial();
        } else {
            manageFactory.reInit(properties);
        }
    }

    public static void setManageFactory(
            ManageFactory manageFactory) {
        Manager.manageFactory = manageFactory;
    }

}
